<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能问答 - AI智能教学培训系统</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css/github-markdown.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4895ef;
            --light: #f8f9fa;
            --dark: #212529;
            --success: #4cc9f0;
            --warning: #f72585;
            --gray: #6c757d;
        }

        body {
            background-color: #f5f7fb;
            color: var(--dark);
        }

        .app-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, var(--primary), var(--secondary));
            color: white;
            box-shadow: 3px 0 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }

        .logo-container {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .logo {
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon {
            margin-right: 10px;
            font-size: 28px;
        }

        .nav-menu {
            padding: 20px 0;
        }

        .nav-item {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            cursor: pointer;
            transition: all 0.3s;
            border-left: 4px solid transparent;
        }

        .nav-item:hover {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-item.active {
            background: rgba(255, 255, 255, 0.1);
            border-left: 4px solid var(--accent);
        }

        .nav-icon {
            margin-right: 12px;
            font-size: 18px;
        }

        /* 主内容区 */
        .main-content {
            flex: 1;
            margin-left: 260px;
            padding: 30px;
            transition: all 0.3s;
            display: flex;
            flex-direction: column;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title {
            font-size: 28px;
            font-weight: 600;
            color: var(--dark);
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--accent);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            margin-right: 10px;
        }

        /* 问答页面特定样式 */
        .qa-container {
            display: flex;
            flex: 1;
            gap: 20px;
        }

        .sidebar-left {
            width: 300px;
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            overflow-y: auto;
        }

        .chat-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            overflow: hidden;
        }

        .chat-header {
            padding: 20px;
            background: var(--primary);
            color: white;
            font-weight: 600;
            display: flex;
            align-items: center;
        }

        .chat-header-icon {
            margin-right: 10px;
            font-size: 20px;
        }

        .chat-messages {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background: #fafafa;
        }

        .message {
            margin-bottom: 20px;
            display: flex;
        }

        .message.bot {
            justify-content: flex-start;
        }

        .message.user {
            justify-content: flex-end;
        }

        .message-avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: var(--accent);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            margin-right: 12px;
        }

        .message.user .message-avatar {
            background: var(--warning);
            margin-right: 0;
            margin-left: 12px;
        }

        .message-content {
            max-width: 70%;
        }

        .message.bot .message-content {
            background: white;
            border: 1px solid #eee;
            border-radius: 0 15px 15px 15px;
            padding: 12px 16px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        }

        .message.user .message-content {
            background: var(--accent);
            color: white;
            border-radius: 15px 0 15px 15px;
            padding: 12px 16px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        }

        .message-time {
            font-size: 12px;
            color: var(--gray);
            margin-top: 5px;
            text-align: right;
        }

        .chat-input {
            display: flex;
            padding: 15px;
            background: white;
            border-top: 1px solid #eee;
        }

        .chat-input input {
            flex: 1;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 25px;
            outline: none;
            transition: all 0.3s;
            font-size: 14px;
        }

        .chat-input input:focus {
            border-color: var(--accent);
            box-shadow: 0 0 0 2px rgba(67, 97, 238, 0.2);
        }

        .chat-input button {
            margin-left: 10px;
            background: var(--primary);
            color: white;
            border: none;
            border-radius: 25px;
            padding: 10px 20px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
        }

        .chat-input button:hover {
            background: var(--secondary);
        }

        .chat-input button .icon {
            margin-right: 5px;
        }

        .section-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
        }

        .section-title .icon {
            margin-right: 8px;
            color: var(--primary);
        }

        .question-list {
            display: flex;
            flex-direction: column;
            gap: 12px;
            margin-bottom: 25px;
        }

        .question-item {
            padding: 12px;
            background: #f8f9fa;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 14px;
        }

        .question-item:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }

        .history-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .history-item {
            padding: 10px;
            border-bottom: 1px solid #eee;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 13px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .history-item:hover {
            background: #f8f9fa;
        }

        .history-time {
            color: var(--gray);
            font-size: 12px;
        }

        .knowledge-resources {
            margin-top: 25px;
        }

        .resource-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .resource-item {
            display: flex;
            align-items: center;
            padding: 10px;
            border-radius: 6px;
            background: #f8f9fa;
            color: var(--dark);
            text-decoration: none;
            transition: all 0.3s;
            font-size: 14px;
        }

        .resource-item:hover {
            background: #e9ecef;
            color: var(--primary);
        }

        .resource-icon {
            margin-right: 8px;
            font-size: 16px;
            color: var(--primary);
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
            }

            .logo-text, .nav-text {
                display: none;
            }

            .logo-icon {
                margin-right: 0;
            }

            .nav-item {
                justify-content: center;
            }

            .nav-icon {
                margin-right: 0;
            }

            .main-content {
                margin-left: 70px;
            }

            .sidebar-left {
                display: none;
            }
        }

        /* 动画效果 */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .fade-in {
            animation: fadeIn 0.5s ease forwards;
        }

        .typing-indicator {
            display: flex;
            padding: 10px;
        }

        .typing-dot {
            width: 8px;
            height: 8px;
            background: #ddd;
            border-radius: 50%;
            margin: 0 2px;
            animation: typingAnimation 1.4s infinite ease-in-out;
        }

        .typing-dot:nth-child(1) {
            animation-delay: 0s;
        }

        .typing-dot:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-dot:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typingAnimation {
            0%, 60%, 100% { transform: translateY(0); }
            30% { transform: translateY(-5px); background: var(--accent); }
        }

        /* 侧边栏链接无下划线且不变色 */
        a.nav-item {
            text-decoration: none;
            color: inherit;
        }
        a.nav-item:visited,
        a.nav-item:active,
        a.nav-item:focus {
            color: inherit;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="app-container">
        <!-- 侧边导航 -->
        <div id="sidebar"></div>

        <!-- 主内容区 -->
        <div class="main-content">
            <!-- 顶部导航 -->
            <div class="header">
                <h1 class="page-title">智能问答</h1>
                <div class="user-info">
                    <div class="avatar">张</div>
                    <div>
                        <div class="user-name">张明</div>
                        <div class="user-role">学生</div>
                    </div>
                </div>
            </div>

            <!-- 问答主区域 -->
            <div class="qa-container">
                <!-- 左侧边栏 -->
                <div class="sidebar-left fade-in">
                    <!-- 常见问题推荐 -->
                    <div class="question-recommend">
                        <div class="section-title">
                            <i class="el-icon-question icon"></i>
                            常见问题推荐
                        </div>
                        <div class="question-list">
                            <div class="question-item" @click="askQuestion('有哪些课程？')">
                                有哪些课程？
                            </div>
                            <div class="question-item" @click="askQuestion('查询我的作业信息')">
                                查询我的作业信息
                            </div>
                            <div class="question-item" @click="askQuestion('查看我的考试成绩')">
                                查看我的考试成绩
                            </div>
                            <div class="question-item" @click="askQuestion('有哪些学习资源？')">
                                有哪些学习资源？
                            </div>
                            <div class="question-item" @click="askQuestion('根据我的成绩推荐一些学习资源')">
                                根据我的成绩推荐一些学习资源
                            </div>
                            <div class="question-item" @click="askQuestion('如何理解矩阵的秩？')">
                                如何理解矩阵的秩？
                            </div>
                            <div class="question-item" @click="askQuestion('什么是二叉搜索树？')">
                                什么是二叉搜索树？
                            </div>
                        </div>
                    </div>

                    <!-- 历史记录 -->
                    <div class="history-section">
                        <div class="section-title">
                            <i class="el-icon-time icon"></i>
                            历史记录
                        </div>
                        <div class="history-list">
                            <div class="history-item" v-for="(item, index) in history" :key="index" @click="loadHistory(item)">
                                <span>{{ item.question }}</span>
                                <span class="history-time">{{ item.time }}</span>
                            </div>
                        </div>
                    </div>

                    <!-- 知识资源 -->
                    <div class="knowledge-resources">
                        <div class="section-title">
                            <i class="el-icon-collection icon"></i>
                            知识资源
                        </div>
                        <div class="resource-list">
                            <a href="#" class="resource-item">
                                <i class="el-icon-notebook-2 resource-icon"></i>
                                高等数学知识库
                            </a>
                            <a href="#" class="resource-item">
                                <i class="el-icon-connection resource-icon"></i>
                                线性代数专题
                            </a>
                            <a href="#" class="resource-item">
                                <i class="el-icon-s-data resource-icon"></i>
                                概率统计手册
                            </a>
                            <a href="#" class="resource-item">
                                <i class="el-icon-cpu resource-icon"></i>
                                数据结构与算法
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 聊天主区域 -->
                <div class="chat-container fade-in">
                    <div class="chat-header">
                        <i class="el-icon-chat-dot-round chat-header-icon"></i>
                        AI学习助手
                    </div>
                    <div class="chat-messages">
                        <div class="message bot">
                            <div class="message-avatar">AI</div>
                            <div class="message-content">
                                你好！我是你的AI学习助手，可以解答你在学习过程中遇到的问题。你可以问我关于课程内容、作业题目或学习方法等方面的问题。
                                <div class="message-time">今天 10:00</div>
                            </div>
                        </div>

                        <div class="message user">
                            <div class="message-content">
                                请帮我解释一下矩阵乘法的概念
                                <div class="message-time">今天 10:02</div>
                            </div>
                            <div class="message-avatar">张</div>
                        </div>

                        <div class="message bot">
                            <div class="message-avatar">AI</div>
                            <div class="message-content">
                                矩阵乘法是线性代数中的基本运算。两个矩阵A和B的乘积C的第i行第j列元素等于A的第i行与B的第j列对应元素的乘积之和。
                                <div class="message-time">今天 10:02</div>
                            </div>
                        </div>

                        <div class="message bot">
                            <div class="message-avatar">AI</div>
                            <div class="message-content">
                                推荐学习资源：<br>
                                - 线性代数第三章：矩阵运算<br>
                                - 矩阵乘法专项练习<br>
                                - 矩阵应用实例解析视频
                                <div class="message-time">今天 10:03</div>
                            </div>
                        </div>

                        <div v-for="(message, index) in messages" :key="index" class="message" :class="message.type">
                            <div class="message-avatar" v-if="message.type === 'bot'">AI</div>
                            <div class="message-content markdown-body">
                                <div v-if="message.type === 'bot'" v-html="renderMarkdown(message.content)"></div>
                                <div v-else>{{ message.content }}</div>
                                <div class="message-time">{{ message.time }}</div>
                            </div>
                            <div class="message-avatar" v-if="message.type === 'user'">{{ userInitial }}</div>
                        </div>

                        <div class="message bot" v-if="isTyping">
                            <div class="message-avatar">AI</div>
                            <div class="message-content">
                                <div class="typing-indicator">
                                    <div class="typing-dot"></div>
                                    <div class="typing-dot"></div>
                                    <div class="typing-dot"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="chat-input">
                        <input
                                type="text"
                                v-model="userInput"
                                placeholder="输入你的问题..."
                                @keyup.enter="sendMessage"
                                :disabled="isTyping">
                        <button @click="sendMessage" :disabled="isTyping || !userInput.trim()">
                            <i class="el-icon-s-promotion icon"></i>
                            发送
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            userInput: '',
            messages: [],
            isTyping: false,
            history: [
                { question: '如何理解矩阵的秩？', time: '昨天 15:30' },
                { question: '微积分基本定理是什么？', time: '昨天 14:12' },
                { question: '二叉搜索树的特点', time: '前天 19:45' }
            ],
            userInitial: '张'
        },
        methods: {
            sendMessage() {
                if (!this.userInput.trim() || this.isTyping) return;

                const question = this.userInput.trim();
                this.addMessage('user', question);
                this.addToHistory(question);
                this.userInput = '';
                this.isTyping = true;

                // 模拟AI回复延迟
                setTimeout(() => {
                    this.getAIResponse(question);
                    this.isTyping = false;
                }, 1500);
            },
            addMessage(type, content) {
                const now = new Date();
                const timeString = `今天 ${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;

                this.messages.push({
                    type,
                    content,
                    time: timeString
                });

                // 滚动到底部
                this.$nextTick(() => {
                    const container = document.querySelector('.chat-messages');
                    container.scrollTop = container.scrollHeight;
                });
            },
            getAIResponse(question) {
                fetch('/ai/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        // 'Authorization': 'Bearer ' + localStorage.getItem('token') // 如有token可加
                    },
                    body: JSON.stringify({
                        question: question,
                        subject: "general",
                        student: {
                            student_id: 1  // 默认学生ID，实际应用中应该从登录信息获取
                        }
                    })
                })
                .then(res => res.json())
                .then(resp => {
                    if (resp.success && resp.data) {
                        this.addMessage('bot', resp.data.answer || 'AI暂无回复');
                    } else {
                        this.addMessage('bot', resp.message || 'AI服务暂时不可用');
                    }
                })
                .catch(() => {
                    this.addMessage('bot', 'AI服务暂时不可用');
                });
            },
            addToHistory(question) {
                const now = new Date();
                const timeString = `今天 ${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;

                this.history.unshift({
                    question,
                    time: timeString
                });

                // 保持历史记录不超过10条
                if (this.history.length > 10) {
                    this.history.pop();
                }
            },
            askQuestion(question) {
                this.userInput = question;
                this.sendMessage();
            },
            loadHistory(item) {
                this.askQuestion(item.question);
            },
            renderMarkdown(content) {
                if (window.marked && window.marked.marked) {
                    return window.marked.marked(content);
                }
                return content;
            }
        }
    });
</script>
<script>
  fetch('sidebar.html').then(r=>r.text()).then(html=>{document.getElementById('sidebar').innerHTML=html;});
</script>
</body>
</html>